<?php
namespace Admin\Model;
use Think\Model;
class CourseModel extends Model
{
	protected $insertFields = array('sort','course_name','course_show_name','start_time','end_time','course_level','course_price','course_exp_price','course_comment','course_addr','classroom_id','teacher_id','cat_id','jifen','is_show','user_count','color','company_id');
	protected $updateFields = array('id','sort','course_name','course_show_name','start_time','end_time','course_level','course_price','course_exp_price','course_comment','course_addr','classroom_id','teacher_id','cat_id','jifen','is_show','user_count','color');
	protected $_validate = array(
		array('sort', 'number', '排序必须是一个整数！', 2, 'regex', 3),
		array('course_name', '1,255', '课程名的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('course_show_name', '1,255', '课程展示名的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('course_level', 'number', '课程等级必须是一个整数！', 2, 'regex', 3),
		array('course_price', '1,255', '课程价格的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('course_exp_price', '1,255', '课程体验价格的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('course_comment', 'require', '课程备注不能为空！', 1, 'regex', 3),
		array('course_addr', 'require', '课程地点不能为空！', 1, 'regex', 3),
		array('classroom_id', 'number', '教室ID必须是一个整数！', 2, 'regex', 3),
		array('teacher_id', 'number', '教师ID必须是一个整数！', 2, 'regex', 3),
		array('cat_id', 'number', '分类ID必须是一个整数！', 2, 'regex', 3),
		array('jifen', 'number', ' 上课积分必须是一个整数！', 2, 'regex', 3),
		array('is_show', 'number', '是否显示必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 15)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($course_name = I('get.course_name'))
			$where['course_name'] = array('like', "%$course_name%");
		if($course_show_name = I('get.course_show_name'))
			$where['course_show_name'] = array('like', "%$course_show_name%");
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['start_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['start_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['start_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['end_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['end_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['end_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
		if($course_exp_price = I('get.course_exp_price'))
			$where['course_exp_price'] = array('like', "%$course_exp_price%");
		if($course_addr = I('get.course_addr'))
			$where['course_addr'] = array('eq', $course_addr);
		if($classroom_id = I('get.classroom_id'))
			$where['classroom_id'] = array('eq', $classroom_id);
		if($teacher_id = I('get.teacher_id'))
			$where['teacher_id'] = array('eq', $teacher_id);
		if($is_show = I('get.is_show'))
			$where['is_show'] = array('eq', $is_show);
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['logo']) && $_FILES['logo']['error'] == 0)
		{
			$ret = uploadOne('logo', 'Course', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['logo'] = $ret['images'][0];
				$data['big_logo'] = $ret['images'][1];
				$data['mid_logo'] = $ret['images'][2];
				$data['sm_logo'] = $ret['images'][3];
			}
			else
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
        $data['company_id'] = session('admin.company_id');
    }
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['logo']) && $_FILES['logo']['error'] == 0)
		{
			$ret = uploadOne('logo', 'Course', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['logo'] = $ret['images'][0];
				$data['big_logo'] = $ret['images'][1];
				$data['mid_logo'] = $ret['images'][2];
				$data['sm_logo'] = $ret['images'][3];
			}
			else
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_logo'),
				I('post.old_big_logo'),
				I('post.old_mid_logo'),
				I('post.old_sm_logo'),

			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('logo,big_logo,mid_logo,sm_logo')->find($option['where']['id']);
		deleteImage($images);
		//删除课程表
        D('course_calendar')->where(['course_id'=>['eq',$option['where']['id']]])->delete();
	}
	/************************************ 其他方法 ********************************************/
}
